package com.qz.mos.contorller;

import com.qz.mos.service.RestDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.MediaType;
import org.springframework.http.MediaTypeFactory;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping(headers = "content-type=application/*" )
@ConditionalOnProperty(name = "spring.cloud.gateway.enabled",havingValue = "false")
public class CacheMosContoller {

    @Autowired
    RestDataService restDataService;
    @Autowired
    ResourceLoader resourceLoader;
    @Value( "${static-locations:resources}" )
    String filePath;



    protected MediaType getMediaType(HttpServletRequest request, Resource resource) {
        MediaType result = null;
        String mimeType = request.getServletContext().getMimeType( resource.getFilename() );
        if (StringUtils.hasText( mimeType )) {
            result = MediaType.parseMediaType( mimeType );
        }
        if (result == null || MediaType.APPLICATION_OCTET_STREAM.equals( result )) {
            MediaType mediaType = null;
            String filename = resource.getFilename();
            String ext = StringUtils.getFilenameExtension( filename );
            if (mediaType == null) {
                mediaType = MediaTypeFactory.getMediaType( filename ).orElse( null );
            }
            if (mediaType != null) {
                result = mediaType;
            }
        }
        return result;
    }
    @GetMapping(value = "/**" )
    public void getData(HttpServletRequest request, ResourceHttpMessageConverter resourceHttpMessageConverter, HttpServletResponse response) throws IOException {
        String requestURI =request.getRequestURI();
        if(requestURI.matches( "/index.html|/static/.*|/favicon.*" )||requestURI.equalsIgnoreCase( "/" )){
            if(requestURI.equalsIgnoreCase( "/" )){
                requestURI="/index.html";
            }
            Resource resource=resourceLoader.getResource( "file:"+ filePath+requestURI);
            if(!resource.exists()){
                restDataService.getData( request,response,resourceHttpMessageConverter );
            }
            ServletServerHttpResponse outputMessage = new ServletServerHttpResponse( response );
            MediaType mediaType = getMediaType( request, resource );
            resourceHttpMessageConverter.write( resource, mediaType, outputMessage );
        }
        restDataService.getData( request,response,resourceHttpMessageConverter );
    }

    @PostMapping("/**")
    public Object postData(HttpServletRequest request){
        Object data = restDataService.postData( request );

        return data;
    }

    @PutMapping("/**")
    public Object putData(HttpServletRequest request){
        Object data = restDataService.postData( request );

        return data;
    }

    @DeleteMapping("/**")
    public Object delData(HttpServletRequest request){
        Object data = restDataService.postData( request );

        return data;
    }
}
